package com.ruoyi.cust.controller;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cust.domain.CityInfo;
import com.ruoyi.cust.service.ICityInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资讯Controller
 *
 * @author cjq
 * @date 2024-12-06
 */
@Api("资讯")
@RestController
@RequestMapping("/city/info")
public class CityInfoController extends BaseController {
    @Autowired
    private ICityInfoService cityInfoService;

    /**
     * 查询资讯列表
     */
    @ApiOperation("查询资讯列表")
//    @PreAuthorize("@ss.hasPermi('city:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(CityInfo cityInfo) {
        startPage();
        List<CityInfo> list = cityInfoService.selectCityInfoList(cityInfo);
        return getDataTable(list);
    }

    /**
     * 导出资讯列表
     */
    @ApiOperation("导出资讯列表")
    @PreAuthorize("@ss.hasPermi('city:info:export')")
    @Log(title = "资讯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityInfo cityInfo) {
        List<CityInfo>      list = cityInfoService.selectCityInfoList(cityInfo);
        ExcelUtil<CityInfo> util = new ExcelUtil<CityInfo>(CityInfo.class);
        util.exportExcel(response, list, "资讯数据");
    }

    /**
     * 获取资讯详细信息
     */
    @ApiOperation("获取资讯详细信息")
    @PreAuthorize("@ss.hasPermi('city:info:query')")
    @GetMapping(value = "/{infoId}")
    public AjaxResult getInfo(@PathVariable("infoId") Long infoId) {
        return success(cityInfoService.selectCityInfoByInfoId(infoId));
    }

    /**
     * 新增资讯
     */
    @ApiOperation("新增资讯")
    @PreAuthorize("@ss.hasPermi('city:info:add')")
    @Log(title = "资讯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityInfo cityInfo) {
        cityInfo.setInfoId(Long.valueOf(IdUtil.getSnowflake(1, 1).nextId()));
        return toAjax(cityInfoService.insertCityInfo(cityInfo));
    }

    /**
     * 修改资讯
     */
    @ApiOperation("修改资讯")
    @PreAuthorize("@ss.hasPermi('city:info:edit')")
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityInfo cityInfo) {
        return toAjax(cityInfoService.updateCityInfo(cityInfo));
    }

    /**
     * 删除资讯
     */
    @ApiOperation("删除资讯")
    @PreAuthorize("@ss.hasPermi('city:info:remove')")
    @Log(title = "资讯", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        return toAjax(cityInfoService.deleteCityInfoByInfoIds(infoIds));
    }
}
